package Util;

import EncapsulatingObjects.User;

import java.sql.*;

public class UserDatabaseUtil {
    public Connection getConnection() throws ClassNotFoundException, SQLException {
        //加载一个驱动器
        Class.forName("org.apache.derby.jdbc.ClientDriver");
        //定义一个链接
        String url = "jdbc:derby://localhost:1527/NIIT";
        String user = "root";
        String password = "root";
        Connection conn = DriverManager.getConnection(url, user, password);
        return conn;
    }

    public void createTable(Connection conn) throws SQLException {
        String str1 = "create table userdata ( " +
                "id int generated always as identity," +
                "account varchar(10) not null," +
                "username varchar(30) not null," +
                "tel varchar(20) not null," +
                "password varchar(20) not null," +
                "birthday date not null," +
                "gender varchar(10) not null," +
                "PersonalSignature varchar(200)," +
                "isAdmin boolean , " +
                "isLogin boolean ," +
                "primary key (id))";
        Statement stmt1 = conn.createStatement();
        stmt1.execute(str1);
        stmt1.close();
    }

    public void insertInfo(Connection conn, User user, boolean statue, boolean isLogin) throws SQLException {
        String str = "Insert into userdata(account, username, tel, password, birthday, " +
                "gender,personalsignature,isAdmin, isLogin)" +
                "Values (?,?,?,?,?,?,?,?,?)";
        PreparedStatement ps = conn.prepareStatement(str);
        ps.setString(1, user.getAccount());
        ps.setString(2, user.getName());
        ps.setString(3, user.getPhone());
        ps.setString(4, user.getPassword());
        ps.setDate(5, Date.valueOf(user.getBirthday()));
        ps.setString(6, user.getGender());
        ps.setString(7, user.getSignature());
        ps.setBoolean(8, statue);
        ps.setBoolean(9, isLogin);
        ps.executeUpdate();
        ps.close();
    }

    public void updatePassword(Connection conn, User user) throws SQLException {
        String str4 = "update userdata set password = '" + user.getPassword() + "' where account = '" + user.getAccount() + "'";
        Statement stmt4 = conn.createStatement();
        stmt4.executeUpdate(str4);
        stmt4.close();
    }

    public void updateInfo(User user) {
        try {
            Connection conn = getConnection();
            String str4 = "update userdata set username = ?, tel = ?, birthday = ?, gender = ?,personalsignature = ?" +
                    " where account = '" + user.getAccount() + "'";
            PreparedStatement ps = conn.prepareStatement(str4);
            ps.setString(1,user.getName());
            ps.setString(2,user.getPhone());
            ps.setDate(3,Date.valueOf(user.getBirthday()));
            ps.setString(4,user.getGender());
            ps.setString(5,user.getSignature());
            ps.executeUpdate();
            conn.close();
            ps.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void updateLoginStatue(User user, boolean isLogin)  {
        Connection conn = null;
        try {
            conn = getConnection();
            String str4 = "update userdata set isLogin = '" + isLogin + "' where account = '" + user.getAccount() + "'";
            Statement stmt4 = conn.createStatement();
            stmt4.executeUpdate(str4);
            stmt4.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public User initUser(User user){
        try {
            Connection conn = getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from userdata");
            while(rs.next()){
                if(rs.getBoolean("isLogin")){
                    user.setId(rs.getInt("id"));
                    user.setAccount(rs.getString("account"));
                    user.setName(rs.getString("username"));
                    user.setPhone(rs.getString("tel"));
                    user.setGender(rs.getString("gender"));
                    user.setBirthday((rs.getDate("birthday")).toLocalDate());
                    user.setSignature(rs.getString("personalSignature"));
                    break;
                }
            }
        } catch (ClassNotFoundException classNotFoundException) {
            classNotFoundException.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return user;
    }

    public void getUserId(User user) {
        try {
            Connection conn = getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from userdata");
            while (rs.next()){
                if(rs.getString("account").equals(user.getAccount())){
                    user.setId(rs.getInt(1));
                    break;
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
